package com.hna.face.shiro;

/**
 * shiro
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.face.model.BfOperation;
import com.hna.face.model.BfUser;
import com.hna.face.service.IBfOperationService;
import com.hna.face.service.IBfRoleService;
import com.hna.face.service.IBfUserService;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service
public class ShiroRealm extends AuthorizingRealm {
  @Autowired
  IBfUserService userService;

  @Autowired
  IBfRoleService roleService;
  
  @Autowired
  IBfOperationService operationService;

  


  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String username = principals.getPrimaryPrincipal().toString();
    Set<String> roles = new HashSet<String>();
    //Set<ParkOperation> opSet=operationService.selectByUserName(username);
    Set<BfOperation> opSet=new HashSet<BfOperation>();
    Set<String> permissions = new HashSet<String>();
    for(BfOperation op:opSet){
    	permissions.add(op.getOpname());
    }

    
    
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.setRoles(roles);
    info.setStringPermissions(permissions);
    return info;
  }


  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
      throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    
    String password = new String((char[])token.getCredentials());
    Wrapper<BfUser> wrapper=new EntityWrapper<BfUser>();
	wrapper.eq("username", token.getUsername());
	
	List<BfUser> userList = userService.selectList(wrapper);
	
    if (userList == null) {
      throw new UnknownAccountException("unknownUserError ");
    }
    if (userList.size() > 1) {
      throw new AuthenticationException("More than one User found for user [" + token.getUsername()
          + "]");
    }
    
    if(!password.equals(userList.get(0).getUserpasswd())){
    	 throw new IncorrectCredentialsException("userOrPasswdError");
    }

    BfUser user = userList.get(0);

    SimpleAuthenticationInfo authenticationInfo =
        new SimpleAuthenticationInfo(user.getUsername(),  user.getUserpasswd(), getName() // realm
                                                                                         // name
        );
    
    return authenticationInfo;
  }

  public void clearCachedAuthorizationInfo(String principal) {
    SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
    clearCachedAuthorizationInfo(principals);
  }
}
